package com.sprocomm.agingtest.ir;

import com.sprocomm.agingtest.BaseActivity;
import com.sprocomm.agingtest.Config;
import com.sprocomm.agingtest.R;
import com.sprocomm.agingtest.Tools;
import com.sprocomm.agingtest.views.CDTTextView;

import android.content.Context;
import android.hardware.ConsumerIrManager;
import android.os.CountDownTimer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IRTestActivity extends BaseActivity {
    private ConsumerIrManager mConsumerIrManager;
    private CDTTextView mCDTTextView;
    private boolean isSuprot = false;
    private Button mBtnStart;

    //编码规则
//    //起始码S电平宽度为：9000us低电平+4500us高电平
//    public static final int startdown = 9000;
//    public static final int startup = 4500;
//
//    //连接码C电平宽度为：600us低电平+20000us高电平
//    public static final int connectdown = 600;
//    public static final int connectup = 20000;
//
//    //数据码由0，1组成：
//    //0的电平宽度为：600us低电平+600us高电平，
//    public static final int zerodown = 600;
//    public static final int zeroup = 600;
//
//    //1的电平宽度为：600us低电平+1600us高电平
//    public static final int onedown = 600;
//    public static final int oneup = 1600;

    //命令格式（数组内的数值拼接）


    //模式自动数组（扫风）开机
//    public static final int[] pattern = {
//            startdown, startup,//起始码
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//1-3
//            onedown, oneup, zerodown, zeroup, zerodown, zeroup,//4-6
//            onedown, oneup, zerodown, zeroup, zerodown, zeroup,//7-9
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//10-12
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//13-15
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//16-18
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//19-21
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//22-24
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//25-27
//            zerodown, zeroup, onedown, oneup, zerodown, zeroup,//28-30
//            onedown, oneup, zerodown, zeroup, zerodown, zeroup,//31-33
//            onedown, oneup, zerodown, zeroup,//34-35  前35位数据码结束
//            connectdown, connectup,//连接码   后32位开始
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//36-38
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//39-41
//            zerodown, zeroup, zerodown, zeroup, onedown, oneup,//42-44
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//45-47
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//48-50
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//51-53
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//54-56
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//57-59
//            zerodown, zeroup, zerodown, zeroup, zerodown, zeroup,//60-62
//            zerodown, zeroup, //63
//            zerodown, zeroup, onedown, oneup, zerodown, zeroup, onedown, oneup,//64-67(四位检验码)后32位结束
//
//    };

    public static final int[] pattern = {
            1000,49000,1000,49000
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_irtest);
        mCDTTextView = (CDTTextView) findViewById(R.id.cdt_timer);

        mCDTTextView.setCountDownTimer(Config.IR_TEST_TIME, 1000);
        mBtnStart = (Button) findViewById(R.id.btn_start);

        mConsumerIrManager = (ConsumerIrManager) getSystemService(Context.CONSUMER_IR_SERVICE);
        if (mConsumerIrManager == null) {
            Tools.showToast(this, "Infra-red is not support");
            failTest();
        } else {
            boolean isIr = mConsumerIrManager.hasIrEmitter();
            if (isIr) {
                isSuprot = true;
            } else {
                Tools.showToast(this, "Infra-red is not support");
                failTest();
            }
        }
    }

    public void start(View view) {
//        mConsumerIrManager.transmit(38000,pattern);
        mCDTTextView.setTimerStart();
        mCountDownTimer.start();
        mBtnStart.setEnabled(false);
    }

    private CountDownTimer mCountDownTimer = new CountDownTimer(Config.IR_TEST_TIME, Config.IR_CHSNGE_TIME) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (mConsumerIrManager != null && isSuprot){
                try{
                    mConsumerIrManager.transmit(38000, pattern);
                }catch(Exception e){
                    e.printStackTrace();
                    Tools.showToast(IRTestActivity.this, "Infra-red is not support");
                    failTest();
                }

            }else{
                failTest();
            }
        }

        @Override
        public void onFinish() {
            setPassBtnEnable(true);
            mBtnStart.setEnabled(true);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCountDownTimer.cancel();
        mCDTTextView.setTimerCancle();
    }

    public void failTest(){
        mCountDownTimer.cancel();
        mCDTTextView.setTimerCancle();
        mCDTTextView.setText("fail");
        mBtnStart.setEnabled(false);
    }
}
